//
//  OauthUtils.m
//  meiricai
//
//  Created by qiuqk on 13-5-11.
//
//

#import "OauthUtils.h"
#import "OAuthConsumer.h"
#import "MRMConfig.h"
#import "MRMSdk.h"

#if TARGET_IPHONE_SIMULATOR
NSString *baseAddress = @"http://test.api.meiricai.com";
#else
NSString *baseAddress = @"http://api.meiricai.com";
#endif

@implementation OauthUtils

+ (id)getFromPublicUrl:(NSString *)path withDict:(NSMutableDictionary *)queryParams
{
    NSArray *allKeys = [queryParams allKeys];
    NSMutableString *urlString = nil;
    for (int i = 0; i < [queryParams count]; i++)
    {
        NSString* key = [allKeys objectAtIndex:i];
        NSString* value = [queryParams objectForKey:key];
        key = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        if(urlString == nil) {
            urlString = [NSMutableString stringWithFormat:@"?%@=%@", key, value];
        }
        else {
            [urlString appendFormat:@"&%@=%@", key, value];
        }
    }
    if(urlString == nil) {
        urlString = [NSMutableString stringWithString:path];
    }
    else {
        urlString = [NSMutableString stringWithFormat:@"%@%@%@", baseAddress, path, urlString];
    }
    
    NSURL *url = [NSURL URLWithString:[MRMSdk signaturePublicUrl:(NSString *)urlString]];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
	
	NSURLResponse *response;
	NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	
	if (error) {
		NSLog(@"%@", [error localizedDescription]);
		return nil;
	}
	
	error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
	
    return result;
}

+ (id)getFromProtectedUrl:(NSString *)path withDict:(NSDictionary *)queryParams resultIsJson:(BOOL)isJson
{
    NSArray *allKeys = [queryParams allKeys];
    NSMutableString *queryString = nil;
    for (int i = 0; i < [queryParams count]; i++)
    {
        NSString *key = [allKeys objectAtIndex:i];
        NSString *value = [queryParams objectForKey:key];
        key = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
		NSString *kv = [NSString stringWithFormat:@"%@=%@", key, value];
		if(queryString == nil) {
            queryString = [NSMutableString stringWithString:kv];
        }
        else {
            [queryString appendFormat:@"&%@", kv];
        }
    }
	
	NSString *urlString;
    if(queryString == nil) {
		urlString = [NSString stringWithFormat:@"%@%@", baseAddress, path];
    }
    else {
       urlString = [NSString stringWithFormat:@"%@%@?%@", baseAddress, path, queryString];
    }
    
    NSURL *url = [NSURL URLWithString:[MRMSdk signatureProtectedUrl:(NSString *)urlString]];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5];
	NSURLResponse *response;
	NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
	
	if (error) {
		NSLog(@"%@", [error localizedDescription]);
		return nil;
	}
	
	id result;
	if (!isJson)
	{
		result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
		return result;
	}
	else
	{
		result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
		return result;
	}
}

@end
